package com.xingji.caixia.fragment;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.xingji.caixia.R;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseFragment extends Fragment {

    ProgressDialog progressDialog ;
    Context self;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        self = getActivity();
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("正在处理...");
    }

    /**
     *
     * @param requestParams
     * @param tag
     */
    public void post(RequestParams requestParams, final Integer tag){
        if (tag == null) return;
        progressDialog.show();
        requestParams.addHeader("Content-Type","application/x-www-form-urlencoded");
        x.http().post(requestParams, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                Log.e("result",result.toString());
                postSuccess(result,tag);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                postError(ex,isOnCallback,tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                progressDialog.dismiss();
                postCancelled(cex);
            }

            @Override
            public void onFinished() {
                progressDialog.dismiss();
            }
        });
    }

    /**
     * 下载文件
     * @param requestParams
     * @param tag
     */
    public void download(RequestParams requestParams,Integer tag){
        x.http().get(requestParams, new Callback.CommonCallback<File>() {
            @Override
            public void onSuccess(File result) {
                Log.e("file",result.getName()+"");
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
                install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(install);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void postSuccess(JSONObject jsonObject,Integer tag){

    }

    public void postError(Throwable ex, boolean isOnCallback, Integer tag){
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }

    public void postCancelled(Callback.CancelledException cex){

    }

    public void postFinished(){

    }



}
